package com.ll.wechat.user.activity;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import android.app.ActionBar;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ll.wechat.R;
import com.ll.wechat.base.activity.BaseActivity;
import com.ll.wechat.base.app.User;
import com.ll.wechat.base.net.NetUtil;
import com.ll.wechat.base.utils.widget.MyToast;
import com.ll.wechat.base.utils.widget.TextWatcherAdapter;
import com.ll.wechat.base.utils.widget.TextWatcherAdapter.TextWatcherListener;

/**
 * 修改昵称界面
 * 
 * @ClassName: ModifySmallNameActivity
 * @author yeliangliang
 * @date 2015-8-10 下午5:22:31
 */
public class ModifySmallNameActivity extends BaseActivity implements TextWatcherListener,
		OnClickListener {

	private EditText edt_name;
	private String name = "";
	private ActionBar actionBar;
	private TextView tv_save;
	private RelativeLayout img_back;
	private AbstractXMPPConnection connection;

	@Override
	public void initView() {
		setContentView(R.layout.activity_modifys_smallname);
		setTitle("更改名字");
		edt_name = (EditText) findViewById(R.id.edt_small_name);
		// 初始化ActionBar
		initActionBar();

	}

	/**
	 * 初始化ActionBar
	 * 
	 * @author yeliangliang
	 * @date 2015-8-11 上午10:09:52
	 * @version V1.0
	 * @return void
	 */
	private void initActionBar() {
		actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.action_modify_small_name);// 自定义ActionBar布局
		tv_save = (TextView) actionBar.getCustomView().findViewById(R.id.tv_actionbar_save);
		tv_save.setOnClickListener(this);
		img_back = (RelativeLayout) actionBar.getCustomView().findViewById(R.id.rl_img_action_back);
		img_back.setOnClickListener(this);
	}

	@Override
	public void initData() {
		connection = User.getInstance().getConnection();
		Intent intent = getIntent();
		edt_name.setText(intent.getExtras().getString("name"));
		edt_name.addTextChangedListener(new TextWatcherAdapter(edt_name, this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Menu menu2 = getMenuInflater().inflate(menuRes, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onTextChanged(EditText view, String text) {
		// edttext改变监听
		tv_save.setEnabled(true);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			Intent intent = new Intent();
			intent.putExtra("isSuccess", false);
			setResult(RESULT_OK, intent);
			finishActivity(ModifySmallNameActivity.this);
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_img_action_back:// 返回按钮
			// 返回上层通知昵称没有修改
			Intent intent = new Intent();
			intent.putExtra("isSuccess", false);
			setResult(RESULT_OK, intent);
			finishActivity(ModifySmallNameActivity.this);
			break;
		case R.id.tv_actionbar_save:// 保存按钮
			if (!NetUtil.isNetConnection()) {
				MyToast.showToast(ModifySmallNameActivity.this, "网络断开连接");
				return;
			}
			// 上传昵称
			upLoadName();
			break;
		default:
			break;
		}
	}

	/**
	 * 上传昵称
	 * 
	 * @author yeliangliang
	 * @date 2015-8-11 上午11:08:47
	 * @version V1.0
	 * @return void
	 */
	private void upLoadName() {
		if (connection != null) {
			name = edt_name.getText().toString().trim();
			if (!TextUtils.isEmpty(name)) {
				VCard vCard = new VCard();
				vCard.setNickName(name);
				try {
					vCard.save(connection);
				} catch (Exception e) {
					MyToast.showToast(ModifySmallNameActivity.this, "昵称修改失败");
					e.printStackTrace();
				}
				MyToast.showToast(ModifySmallNameActivity.this, "昵称修改成功");
				// 返回上层，传回数据
				Intent intent2 = new Intent();
				intent2.putExtra("isSuccess", true);
				intent2.putExtra("name", name);
				setResult(RESULT_OK, intent2);
				finishActivity(ModifySmallNameActivity.this);
				return;
			}
			MyToast.showToast(ModifySmallNameActivity.this, "昵称不可为空");
		}
		Log.e("ModifySmallNameActivity", "connection为null");
		MyToast.showToast(ModifySmallNameActivity.this, "已断开连接");
	}
}
